{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simple 2 bus Example\n",
    "\n",
    "This tutorial shows how to perform three phase load flow in an unbalanced system.\n",
    "<img src=\"pics/Two bus system.png\" width=\"80%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Three phase or unbalanced load flow is performed using the function runpp_3ph(net)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandapower.create import (\n",
    "    create_empty_network,\n",
    "    create_bus,\n",
    "    create_ext_grid,\n",
    "    create_line,\n",
    "    create_asymmetric_load,\n",
    "    create_transformer_from_parameters,\n",
    "    create_line_from_parameters\n",
    ")\n",
    "from pandapower.pf.runpp_3ph import runpp_3ph\n",
    "from pandapower.std_types import create_std_type, add_zero_impedance_parameters\n",
    "from pandapower.auxiliary import get_free_id"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create Buses\n",
    "After this import we can start by creating an empty network and two 110 kV buses \n",
    "\n",
    "# Create External Grid\n",
    "For negative and zero sequence networks. \n",
    "The following data is needed in addition to a voltage reference.\n",
    "- s_sc_max_mva\n",
    "- rx_max\n",
    "- r0x0_max\n",
    "- x0x_max\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-23T12:11:23.962472Z",
     "start_time": "2025-10-23T12:11:23.826088Z"
    }
   },
   "outputs": [],
   "source": [
    "net = create_empty_network(sn_mva = 100 )\n",
    "bus_eg  =  create_bus(net, vn_kv = 110, name = \"Ext. Grid\")\n",
    "bus_load  =  create_bus(net, vn_kv = 110, name = \"Load\")\n",
    "\n",
    "create_ext_grid(net, bus=bus_eg, vm_pu= 1.0, name=\"Grid Connection\",\n",
    "                   s_sc_max_mva=5000,rx_max=0.1,r0x0_max= 0.1,x0x_max=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creating Lines and transformers\n",
    "\n",
    "Just like balanced load flow there are two ways of creating lines and transformers:\n",
    "- Create using a Standard type from standard type library/ Customized standard type\n",
    "- Create directly from parameters\n",
    "\n",
    "# Create element using a Customized Standard type \n",
    "\n",
    "**This is useful for large networks with many lines and transformers**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-23T12:11:23.978462Z",
     "start_time": "2025-10-23T12:11:23.969459Z"
    }
   },
   "outputs": [],
   "source": [
    "#Line Type created\n",
    "create_std_type(net, {\"r0_ohm_per_km\": 0.0848, \"x0_ohm_per_km\": 0.4649556, \"c0_nf_per_km\":\\\n",
    "    230.6,\"max_i_ka\": 0.963, \"r_ohm_per_km\": 0.0212, \"x_ohm_per_km\": 0.1162389,\n",
    "             \"c_nf_per_km\":  230}, name=\"example_type\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After a Standard type has been created with zero sequence parameters, there are two more steps required:\n",
    "- Create a line using **create_line** with the standard type\n",
    "- Use **add_zero_impedance_parameters** function to insert them into network elements line and trafo from standard types\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-23T12:11:24.010001Z",
     "start_time": "2025-10-23T12:11:23.986465Z"
    }
   },
   "outputs": [],
   "source": [
    "# Actual line created with the length of the line in km\n",
    "create_line(net, from_bus = bus_eg, to_bus = bus_load, length_km = 50.0, std_type=\"example_type\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create Unbalanced Load\n",
    "We can create three phase loads by specifying P, Q values in each phase.\n",
    "The default values are set to zero.\n",
    "\n",
    "Connection type of load can be specified while creating a three phase load\n",
    "\n",
    "**'wye' (Default)** - 3 phase phase-earth load\n",
    "\n",
    "**'delta'** - 3 phase delta load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-23T12:11:24.024986Z",
     "start_time": "2025-10-23T12:11:24.016988Z"
    }
   },
   "outputs": [],
   "source": [
    "#This function creates an unbalanced load\n",
    "create_asymmetric_load(net, bus_load, p_a_mw=50, q_a_mvar=50, p_b_mw=10, q_b_mvar=15, p_c_mw=10, q_c_mvar=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# add_zero_impedance_parameters\n",
    "\n",
    "This function is required when lines and transformers are created using standard types.\n",
    "\n",
    "This adds the zero sequence parameters from standard type to the network elements trafo and line\n",
    "\n",
    "# runpp_3ph\n",
    "\n",
    "This function performs the calculation and returns results in result tables:\n",
    "\n",
    "-res_element_3ph "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-23T12:11:26.943790Z",
     "start_time": "2025-10-23T12:11:24.036987Z"
    }
   },
   "outputs": [],
   "source": [
    "add_zero_impedance_parameters(net)\n",
    "runpp_3ph(net)\n",
    "print(net.trafo)\n",
    "print(\"----------------\")\n",
    "net.res_bus_3ph"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The convergence of load flow can be checked with the following:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-23T12:11:27.271825Z",
     "start_time": "2025-10-23T12:11:27.261430Z"
    }
   },
   "outputs": [],
   "source": [
    "net['converged']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3 Bus system with a transformer in between External Grid and Distribution system\n",
    "\n",
    "<img src=\"pics/Three bus system.png\" width=\"80%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-23T12:11:27.493974Z",
     "start_time": "2025-10-23T12:11:27.318397Z"
    }
   },
   "outputs": [],
   "source": [
    "net = create_empty_network(sn_mva=100)\n",
    "\n",
    "bus_eg = create_bus(net, 10)\n",
    "bus_lv = create_bus(net, 0.4)\n",
    "bus_load = create_bus(net, 0.4 )\n",
    "\n",
    "create_ext_grid(net, bus_eg, s_sc_max_mva=10000, rx_max=0.1)\n",
    "net.ext_grid[\"r0x0_max\"] = 0.1\n",
    "net.ext_grid[\"x0x_max\"] = 1.0\n",
    "print(net.trafo)\n",
    "print('-----------------------------------')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create line and transformer using parameters.\n",
    "\n",
    "Creates a transformer using parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-23T12:11:27.540853Z",
     "start_time": "2025-10-23T12:11:27.506814Z"
    }
   },
   "outputs": [],
   "source": [
    "create_transformer_from_parameters(net, hv_bus=bus_eg, lv_bus=bus_lv,\n",
    "                                          sn_mva= 1.6, vn_hv_kv= 10, \n",
    "                                          vn_lv_kv= 0.4, vk_percent= 6, \n",
    "                                          vkr_percent= 0.78125, pfe_kw= 2.7, \n",
    "                                          i0_percent= 0.16875, shift_degree= 0, \n",
    "                                          tap_side= 'lv',tap_neutral= 0,\n",
    "                                          tap_min= -2, tap_max= 2,\n",
    "                                          tap_step_degree= 0,\n",
    "                                          tap_step_percent= 2.5,\n",
    "                                          tap_changer_type=\"Ratio\",\n",
    "                                          vk0_percent= 6, vkr0_percent= 0.78125, \n",
    "                                          mag0_percent= 100, mag0_rx= 0.,\n",
    "                                          si0_hv_partial= 0.9,vector_group= \"Dyn\",\n",
    "                                          parallel=1,tap_pos=0,\n",
    "                                          index=get_free_id(net.trafo)+1,\n",
    "                                           )\n",
    "create_line_from_parameters(net, bus_lv, bus_load, length_km=0.5,r_ohm_per_km= 0.1941, x_ohm_per_km= 0.07476991,\n",
    "                    c_nf_per_km= 1160., max_i_ka= 0.421,\n",
    "                    endtemp_degree= 70.0, r0_ohm_per_km= 0.7766,\n",
    "                    x0_ohm_per_km= 0.2990796,\n",
    "                    c0_nf_per_km=  496.2 ,index=get_free_id(net.line)+1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " Reset index of transformer to prevent KeyError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-23T12:11:27.555816Z",
     "start_time": "2025-10-23T12:11:27.546821Z"
    }
   },
   "outputs": [],
   "source": [
    "net.trafo.reset_index(inplace=True, drop=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create  Asymmetric load\n",
    "\n",
    "Just like before create three phase load with three phase power values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-23T12:11:27.586839Z",
     "start_time": "2025-10-23T12:11:27.561823Z"
    }
   },
   "outputs": [],
   "source": [
    "#Creates a 3 phase wye load\n",
    "create_asymmetric_load(net, bus_load, p_a_mw=0.0300, q_a_mvar=0.0048, p_b_mw=0.0280, q_b_mvar=0.0036,\n",
    "                               p_c_mw=0.027, q_c_mvar=0.0043,type='wye')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run Asymmetric load flow for the network\n",
    "\n",
    "Same as before, add zero sequence parameters and run 3 phase load flow.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-23T12:11:27.693324Z",
     "start_time": "2025-10-23T12:11:27.594845Z"
    }
   },
   "outputs": [],
   "source": [
    "add_zero_impedance_parameters(net)\n",
    "runpp_3ph(net)\n",
    "net.res_bus_3ph"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
